package com.web.accountingEntry.controller;

import com.web.accountingEntry.entity.AccountingEntry;
import com.web.accountingEntry.entity.AccountingJournal;
import com.web.accountingEntry.service.AccountingEntryService;
import com.web.common.BaseResult;
import com.web.common.enums.BaseStatus;
import com.web.position.entity.Position;
import com.web.transaction.entity.Transaction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "API - AccountingEntryController",description = "accountingEntry")
@RestController
@RequestMapping("/accountingEntry")
@CrossOrigin
public class AccountingEntryController {

    @Autowired
    private AccountingEntryService accountingEntryService;

    @RequestMapping(value = "/addAccountingEntry",method = RequestMethod.POST)
    @ApiOperation(value = "新增accountingEntry")
    public void addAccountingEntry(@RequestBody AccountingEntry ae){
        accountingEntryService.addAccountingEntry(ae);
    }


    @RequestMapping(value = "/addaccountingEntries",method = RequestMethod.POST)
    @ApiOperation(value = "新增accountingEntries")
    public BaseResult addaccountingEntries(@RequestBody Transaction tr){

        List<AccountingJournal> accountingJournals = accountingEntryService.searchFromAccountingTransaction(tr);
        if (accountingJournals.isEmpty()){
            return new BaseResult<>(BaseStatus.ACCOUNTING_NOT_EXIST.getCode(),BaseStatus.ACCOUNTING_NOT_EXIST.getDesc());
        }
        accountingEntryService.saveAccsFromTransaction(accountingJournals,tr);
        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.SUCCESS.getDesc());

    }

    @GetMapping(value = "/getAccountingEcharts")
    public BaseResult getAccountingEcharts(){

        List<Map<String,String>> accountingEntries = accountingEntryService.getAccountingEntries();

        return new BaseResult(BaseStatus.SUCCESS.getCode(),accountingEntries);

    }
}
